package com.ryan.route.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.route.entity.TravelRoute;
import com.ryan.route.service.TravelRouteService;
import com.ryan.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 旅游路线 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2020-07-24
 */
@RestController
@RequestMapping("/route/route")
public class TravelRouteController {

    @Autowired
    TravelRouteService routeService;

    /**
     * 添加路线基本信息
     * @param route
     * @return
     */
    @PostMapping("/addRoute")
    public R addRoute(@RequestBody TravelRoute route){
        String id = routeService.saveRouteInfo(route);
        return R.ok().data("id", id);
    }

    /**
     * 更新路线基本信息
     * @param route
     * @return
     */
    @PostMapping("/updateRoute")
    public R updateRoute(@RequestBody TravelRoute route){
        boolean update = routeService.updateById(route);
        return update?R.ok():R.error();
    }

    /**
     * 根据id获取route
     * @param id
     * @return
     */
    @GetMapping("/getRouteById/{id}")
    public R getRouteById(@PathVariable String id){
        TravelRoute route = routeService.getById(id);
        return R.ok().data("route", route);
    }

    /**
     * 分页+条件查询获取全部路线信息
     * @return
     */
    @GetMapping("/getAllRoute/{pageIndex}/{pageSize}")
    public R getAllRoute(@PathVariable long pageIndex,
                         @PathVariable long pageSize,
                         @RequestParam(required = false) String title){
        Map<String, Object> map = routeService.queryPage(pageIndex, pageSize, title);
        return R.ok().data("list", map.get("list")).data("totalPage", map.get("total"));
    }

    /**
     * 根据id删除路线
     * @param routeIds
     * @return
     */
    @PostMapping("/deleteRoute")
    public R deleteRoute(@RequestBody Long[] routeIds){
        boolean delete = routeService.removeByIds(Arrays.asList(routeIds));
        return delete?R.ok():R.error();
    }

}

